package com.android.cl.coludtf.activity.webview;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.android.cl.coludtf.R;
import com.android.cl.coludtf.activity.base.BaseActivity;
import com.android.cl.coludtf.activity.webview.dsbridge.DJWebView;
import com.android.cl.coludtf.activity.webview.dsbridge.JsApi;
import com.android.cl.coludtf.activity.webview.dsbridge.JsEchoApi;
import com.android.cl.coludtf.activity.webview.dsbridge.OnReturnValue;
import com.android.cl.coludtf.model.BaseBO;
import com.android.cl.coludtf.model.NinePalaces;
import com.android.cl.coludtf.utils.dialog.DialogHelper;
import com.android.cl.coludtf.utils.dialog.inf.OnDialogCancelListener;
import com.android.cl.coludtf.utils.sharedPreferences.DatabaseHelper;
import com.android.cl.coludtf.utils.toast.ToastUtil;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
 * 支持Android5.0.1 以上版本
 */
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class Htmlview extends BaseActivity implements OnDialogCancelListener {
    String TAG = "jsbridge";
    //资源地址
    private static String local = "file:////android_asset/";
    private static String partyBuildApi = local + "PartyBuildApi.js";
    /**
     * js和原始代码识别URL
     */
    private static String uri = "djwebviewjs::";
    BaseBO bo;
    @BindView(R.id.Img_close)
    ImageView ImgClose;
    @BindView(R.id.Img_Back)
    ImageView ImgBack;
    @BindView(R.id.Wv_html)
    DJWebView WvHtml;
    @BindView(R.id.addValue)
    Button addValue;
    @BindView(R.id.append)
    Button append;
    @BindView(R.id.startTimer)
    Button startTimer;
    @BindView(R.id.synAddValue)
    Button synAddValue;
    @BindView(R.id.synGetInfo)
    Button synGetInfo;
    @BindView(R.id.asynAddValue)
    Button asynAddValue;
    @BindView(R.id.asynGetInfo)
    Button asynGetInfo;
    @BindView(R.id.hasMethodAddValue)
    Button hasMethodAddValue;
    @BindView(R.id.hasMethodXX)
    Button hasMethodXX;
    @BindView(R.id.hasMethodAsynAddValue)
    Button hasMethodAsynAddValue;
    @BindView(R.id.hasMethodAsynXX)
    Button hasMethodAsynXX;
    @BindView(R.id.Txt_HtmlJs)
    TextView TxtHtmlJs;
    @BindView(R.id.LL_HtmlJs)
    LinearLayout LLHtmlJs;
    private DialogHelper mDialogHelper;

    @BindView(R.id.Txt_Title)
    TextView TxtTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_htmlview);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    private void initView() {
        bo = DatabaseHelper.getBaseBO();
        if (mDialogHelper == null) {
            mDialogHelper = new DialogHelper(Htmlview.this, this);
        }

        WvHtml.setWebContentsDebuggingEnabled(true);
        WvHtml.addJavascriptObject(new JsApi(), null);
        WvHtml.addJavascriptObject(new JsEchoApi(), "echo");
    }

    private void initData() {
        Intent intent = getIntent();
        NinePalaces ninePalaces = (NinePalaces) intent.getSerializableExtra("data");
        if (ninePalaces != null) {
            if (ninePalaces.getType() == 1) {
                TxtHtmlJs.setVisibility(View.GONE);
                LLHtmlJs.setVisibility(View.GONE);
            }
            TxtTitle.setText(ninePalaces.getText());
            showUrl(ninePalaces.getUrl().trim());
        }
    }

    private void showUrl(String url) {
        WvHtml.loadUrl(url);
        //系统默认会通过手机浏览器打开网页，为了能够直接通过WebView显示网页，则必须设置
        WvHtml.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                //打开时显示加载框
                mDialogHelper.showLoadingDialog("正在加载网页...");
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mDialogHelper.dismissDialog();
            }

        });
    }

    @Override
    public void onDialogCancelListener(AlertDialog dialog) {

    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    void showToast(Object o) {
        ToastUtil.showTextLong("调用JS方法成功：" + o.toString());
    }

    @OnClick({R.id.Img_close, R.id.Img_Back, R.id.addValue, R.id.append, R.id.startTimer, R.id.synAddValue, R.id.synGetInfo, R.id.asynAddValue, R.id.asynGetInfo, R.id.hasMethodAddValue, R.id.hasMethodXX, R.id.hasMethodAsynAddValue, R.id.hasMethodAsynXX})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Img_close:
                finish();
                break;
            case R.id.Img_Back:
                if (WvHtml.canGoBack()) {
                    WvHtml.goBack();//返回上个页面
                    return;
                } else {
                    this.finish();
                }
                break;
            case R.id.addValue:
                WvHtml.callHandler("addValue", new Object[]{3, 4}, new OnReturnValue<Integer>() {
                    @Override
                    public void onValue(Integer retValue) {
                        showToast(retValue);
                    }
                });
                break;
            case R.id.append:
                WvHtml.callHandler("append", new Object[]{"I", "love", "you"}, new OnReturnValue<String>() {
                    @Override
                    public void onValue(String retValue) {
                        showToast(retValue);
                    }
                });
                break;
            case R.id.startTimer:
                WvHtml.callHandler("startTimer", new OnReturnValue<Integer>() {
                    @Override
                    public void onValue(Integer retValue) {
                        showToast(retValue);
                    }
                });
                break;
            case R.id.synAddValue:
                WvHtml.callHandler("syn.addValue", new Object[]{5, 6}, new OnReturnValue<Integer>() {
                    @Override
                    public void onValue(Integer retValue) {
                        showToast(retValue);
                    }
                });
                break;
            case R.id.synGetInfo:
                WvHtml.callHandler("syn.getInfo", new OnReturnValue<JSONObject>() {
                    @Override
                    public void onValue(JSONObject retValue) {
                        showToast(retValue);
                    }
                });
                break;
            case R.id.asynAddValue:
                WvHtml.callHandler("asyn.addValue", new Object[]{5, 6}, new OnReturnValue<Integer>() {
                    @Override
                    public void onValue(Integer retValue) {
                        showToast(retValue);
                    }
                });
                break;
            case R.id.asynGetInfo:
                WvHtml.callHandler("asyn.getInfo", new OnReturnValue<JSONObject>() {
                    @Override
                    public void onValue(JSONObject retValue) {
                        showToast(retValue);
                    }
                });
                break;
            case R.id.hasMethodAddValue:
                WvHtml.hasJavascriptMethod("addValue", new OnReturnValue<Boolean>() {
                    @Override
                    public void onValue(Boolean retValue) {
                        showToast(retValue);
                    }
                });
                break;
            case R.id.hasMethodXX:
                WvHtml.hasJavascriptMethod("XX", new OnReturnValue<Boolean>() {
                    @Override
                    public void onValue(Boolean retValue) {
                        showToast(retValue);
                    }
                });
                break;
            case R.id.hasMethodAsynAddValue:
                WvHtml.hasJavascriptMethod("asyn.addValue", new OnReturnValue<Boolean>() {
                    @Override
                    public void onValue(Boolean retValue) {
                        showToast(retValue);
                    }
                });
                break;
            case R.id.hasMethodAsynXX:
                WvHtml.hasJavascriptMethod("asyn.XX", new OnReturnValue<Boolean>() {
                    @Override
                    public void onValue(Boolean retValue) {
                        showToast(retValue);
                    }
                });
                break;
        }
    }

}
